package br.com.dataphonebr;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import br.com.dataphonebr.entity.Atividade;
import br.com.dataphonebr.entity.Credential;
import br.com.dataphonebr.entity.Grupo;
import br.com.dataphonebr.service.GrupoService;
import br.com.dataphonebr.util.Util;

public class DashboardActivity extends GenericActivity<Atividade> implements android.view.View.OnClickListener, OnClickListener{
	
	private TextView userLogged;
	private GrupoService grupoService;

	
	public DashboardActivity() {
		super(Atividade.class);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dashboard);
		init();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public void init() {
		grupoService = new GrupoService(this);
		super.init();
	}
	
	@Override
	protected void onDestroy() {
		boolean connected = getPreferences(this).getBoolean(SettingsActivity.MANTER_CONECTADO, false);
		if (!connected)
			credentialsService.logout();
		super.onDestroy();
	}
	
	public void logoutOnClick(View view){
		credentialsService.logout();
		openView(DashboardActivity.this, MainActivity.class, true);
	}
	
	@Override
	public void prepareComponents() {
		userLogged = (TextView) findViewById(R.id.userLogged);
		Credential credential = credentialsService.getCredential();
		if (credential == null){
			userLogged.setText(getString(R.string.any_user_connected));
		} else {
			userLogged.setText(getString(R.string.userLabel)+": "+credential.getName());
		}
		super.prepareComponents();
	}
	
	public void optionOnClick(View view) throws NameNotFoundException{
		switch (view.getId()){
			case R.id.activitiesBtn : {
				List<Grupo> grupos = grupoService.selectAll();
				if (grupos != null && !grupos.isEmpty()){
					Intent intent = new Intent(DashboardActivity.this, AppointmentActivity.class);
					intent.putExtra(Util.COMING_FROM, R.layout.activity_dashboard);
					startActivity(intent);
				} else {
					util.showToastMessageLong(getString(R.string.msg_no_group_registered), this);
				}
			}
			break;

			case R.id.controllerBtn : openView(DashboardActivity.this, AdministratorListActivity.class, false);
			break;

			case R.id.settingsBtn : openView(DashboardActivity.this, SettingsActivity.class, false);
			break;

			case R.id.groupBtn : openView(DashboardActivity.this, AppointmentGroupListActivity.class, false);
			break;

			case R.id.logoffBtn : {
				Dialog dialog = new Dialog(view.getContext());
				dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
				dialog.setCancelable(true);
				dialog.setContentView(R.layout.about_popup);
				PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
				TextView version = (TextView)dialog.findViewById(R.id.appVersion);
				version.setText(getString(R.string.version)+pInfo.versionName);
				dialog.show();
			}
			break;
		}
		
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		switch (which) {
			case 0:
				openView(DashboardActivity.this, AppointmentActivity.class, false);
				break;
			case 1:
			case 2:
			case 4: {
				AlertDialog alertDialog = super.createDialog(null, this, new CharSequence[0]);
				alertDialog.setMessage("Building...");
				alertDialog.show();
			}
				break;
			case 3: openView(DashboardActivity.this, FinancialTransactionActivity.class, false);
			break;
		}	
	}

	@Override
	public void onClick(View v) {
		alertDialog.setCanceledOnTouchOutside(true);
		alertDialog.show();
	}

	
}
